import FlyIO from "flyio/dist/npm/wx";
import store from "../store";

// 1. 创建一个FlyIO实例
const http = new FlyIO();

// 2. 配置基础地址和请求超时时间
http.config.baseURL = "https://api-hmugo-web.itheima.net/api/public/v1";
http.config.timeout = 30000;

// 3. 配置请求拦截器
http.interceptors.request.use(
  (config) => {
    // 3.0 判断是否需要携带token
    if (config.url.includes("/my/")) {
      config.headers.Authorization = store.state.user.token;
      console.log("------携带token------");
    }
    // 3.1 请求loading动画
    uni.showLoading({
      title: "正在加载中...",
      mask: true,
    });
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

// 4. 配置响应拦截器
http.interceptors.response.use(
  (res) => {
    uni.hideLoading();
    return res.data;
  },
  (error) => {
    uni.hideLoading();
    return Promise.reject(error);
  }
);

// 5. 导出请求实例
export default http;
